﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace WUSTL.SharePoint.WebParts.AnonInfoPath.FormViewWebPart
{
    [ToolboxItemAttribute(false)]
    public class FormViewWebPart : WebPart
    {
        public FormViewWebPart()
        {
            this.ChromeType = PartChromeType.None;
        }

        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WUSTL.SharePoint.WebParts.AnonInfoPath/AnonFormViewWebPart/AnonFormViewWebPartUserControl.ascx";

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("XSN Template Location"),
            WebDescription("The server relative path to the form template"),
            DefaultValue("")]
        public string xsnURI { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Submit Location"),
            WebDescription("The server relative path to the list or form library"),
            DefaultValue("")]
        public string submitLocation { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("CSS Location"),
            WebDescription("The server relative path to the CSS for this form"),
            DefaultValue("")]
        public string cssLocation { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Success Action"),
            WebDescription("Action taken after the form is successfully submitted")]
        public string successDdl { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Custom HTML"),
            WebDescription("HTML that will be displayed after a success if 'Custom' is set for the Success Action"),
            DefaultValue("")]
        public string customHtml { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Success Page"),
            WebDescription("The server relative path to the page to redirect to after a success if 'Redirect' is set for the Success Action"),
            DefaultValue("")]
        public string redirectUrl { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Send as Email"),
            WebDescription("After a successful submission, send the contents of the form in an email."),
            DefaultValue(false)]
        public bool sendAsEmail { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Enable Query String"),
            WebDescription("Enable the use of a query string parameter."),
            DefaultValue(false)]
        public bool enableQueryString { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Query String Parameter"),
            WebDescription("The query string parameter that will be passed in the URL."),
            DefaultValue("")]
        public string queryStringParameter { get; set; }

        [WebBrowsable(false),
            Category("Form Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Query String Form Field"),
            WebDescription("The field in the form that will receive the query string value."),
            DefaultValue("")]
        public string queryStringFormField { get; set; }

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            base.CreateChildControls();
            if (control != null)
            {
                ((FormViewWebPartUserControl)control).WebPartControl = this;
            }
            Controls.Add(control);
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> epList = new List<EditorPart>();
            AnonInfoPathEditor editorPart = new AnonInfoPathEditor();
            editorPart.ID = this.ID + "_anonInfoPathEditorPart";
            editorPart.Title = "Form Customization";
            editorPart.ChromeState = PartChromeState.Normal;
            epList.Add(editorPart);

            return new EditorPartCollection(base.CreateEditorParts(), epList);
        }
    }
}
